/* **********************************************************************************
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* This source code is subject to terms and conditions of the Shared Source License
* for DSL Editor PowerToy. A copy of the license can be found in the License.htm file
* at the root of this distribution. If you can not locate the Shared Source License
* for DSL Editor PowerToy, please obtain a copy from: http://www.codeplex.com/dsltreegrideditor/Project/License.aspx.
* By using this source code in any fashion, you are agreeing to be bound by
* the terms of the Shared Source License for DSL Editor PowerToy.
*
* You must not remove this notice, or any other, from this software.
*
* **********************************************************************************/
using System;
using System.Collections.Specialized;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.IO;
using System.Text;

using EnvDTE;
using EnvDTE80;

using Microsoft.Practices.Common;
using Microsoft.Practices.Common.Services;
using Microsoft.Practices.ComponentModel;
using Microsoft.Practices.RecipeFramework.Library;

namespace DslEditorPowerToy.Recipes.Converters
{
    public abstract class ConverterBase : TypeConverter, IAttributesConfigurable
    {
        private EnvDTE.DTE _dte = null;
        private ExpressionEvaluationService _evaluator = null;

        #region Constructors
        protected ConverterBase()
            : base()
        {
            this._evaluator = new ExpressionEvaluationService();
        }
        #endregion

        #region IAttributesConfigurable Members
        public virtual void Configure(StringDictionary attributes)
        {
        }

        #endregion

        #region Protected Methods
        /// <summary>
        /// Gets teh VS automation object
        /// </summary>
        [CLSCompliant(false)]
        protected EnvDTE.DTE GetDTE(IServiceProvider serviceProvider)
        {
            if (null == this._dte)
                this._dte = (EnvDTE.DTE)serviceProvider.GetService(typeof(EnvDTE.DTE));

            return this._dte;
        }
        /// <summary>
        /// Gets path to this guidance package
        /// </summary>
        protected string GetPackagePath(IServiceProvider serviceProvider)
        {
            TypeResolutionService resolution = (TypeResolutionService)serviceProvider.GetService(typeof(ITypeResolutionService));
            return resolution.BasePath;
        }
        /// <summary>
        /// Returns the path to this guidance package 'templates' directory
        /// </summary>
        protected string GetPackageTemplatePath(IServiceProvider serviceProvider)
        {
            return Path.Combine(this.GetPackagePath(serviceProvider), "Templates");
        }
        /// <summary>
        /// Evaluates the specified parameter using dictionary of all arguments of the template
        /// </summary>
        /// <param name="argument"></param>
        /// <returns></returns>
        protected object Evaluate(IServiceProvider serviceProvider, string parameter)
        {
            //Get all arguments
            IDictionaryService dictionaryService = (IDictionaryService)serviceProvider.GetService(typeof(IDictionaryService));

            //Evaluate
            return this._evaluator.Evaluate(parameter, new ServiceAdapterDictionary(dictionaryService));
        }
        /// <summary>
        /// Returns the value of the specified argument
        /// </summary>
        /// <param name="_newValue"></param>
        /// <returns></returns>
        protected object GetArgumentValue(IServiceProvider serviceProvider, string argumentName)
        {
            //Get all arguments in template
            IDictionaryService dictionaryService = (IDictionaryService)serviceProvider.GetService(typeof(IDictionaryService));

            return dictionaryService.GetValue(argumentName);
        }
        #endregion

    }
}
